﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GDFlacTool.Common;

namespace GDFlacTool.Forms
{
    public partial class FormArtistGenreNew : Form
    {
        private readonly int _id;
        private ArtistGenre _artistGenre;
        private List<ArtistGenre> _list; 

        public FormArtistGenreNew(int id, List<ArtistGenre> list)
        {
            InitializeComponent();

            _id = id;

            _list = list;

            LoadGenreCombo();
        }

        public ArtistGenre ArtistGenre
        {
            get { return _artistGenre; }
            set { _artistGenre = value; }
        }

        private void FormArtistGenreEdit_Load(object sender, EventArgs e)
        {
            txtArtist.Text = string.Empty;
            cboGenre.Text = string.Empty;

            txtId.Text = string.Format("{0:0000#}", _id);
        }

        private void LoadGenreCombo()
        {
            string[] genres = Genres.Audio;

            for (int i = 0; i < Genres.Audio.Length; i++)
            {
                cboGenre.Items.Add(genres[i]);
            }
        }

        private bool CheckIfExists(string artist)
        {
            ArtistGenre artistGenre = _list.Find(p => p.Artist == artist);

            if (artistGenre == null)
                return false;
            if (artistGenre.Artist == artist)
                return true;
            return false;
        }

        private void txtArtist_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtArtist.Text) && !string.IsNullOrWhiteSpace(txtArtist.Text) && !string.IsNullOrEmpty(cboGenre.Text) && !string.IsNullOrWhiteSpace(cboGenre.Text))
            {
                if (CheckIfExists(txtArtist.Text))
                {
                    btnOK.Enabled = false;
                    txtArtist.ForeColor = Color.Red;
                }
                else
                {
                    btnOK.Enabled = true;
                    txtArtist.ForeColor = Color.Black;
                }
            }
            else
            {
                btnOK.Enabled = false;
            }
        }

        private void cboGenre_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cboGenre.Text) && !string.IsNullOrWhiteSpace(cboGenre.Text) && !string.IsNullOrEmpty(txtArtist.Text) && !string.IsNullOrWhiteSpace(txtArtist.Text))
            {
                if (CheckIfExists(txtArtist.Text))
                {
                    btnOK.Enabled = false;
                    txtArtist.ForeColor = Color.Red;
                }
                else
                {
                    btnOK.Enabled = true;
                    txtArtist.ForeColor = Color.Black;
                }
            }
            else
            {
                btnOK.Enabled = false;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            var ag = new ArtistGenre
                {
                    Id = int.Parse(txtId.Text),
                    Artist = txtArtist.Text,
                    Genre = cboGenre.Text,
                    Status = ArtistGenreStatus.New,
                    Date = string.Format("{0}", DateTime.Now)
                };

            ArtistGenre = ag;
        }
    }
}
